package ar.edu.itba.pod.Legajo47342.simulation;

import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import ar.edu.itba.pod.simul.simulation.Simulation;
import ar.edu.itba.pod.simul.simulation.SimulationEvent;
import ar.edu.itba.pod.simul.simulation.SimulationEventHandler;
import ar.edu.itba.pod.simul.simulation.SimulationInspector;
import ar.edu.itba.pod.simul.simulation.SimulationManager;
import ar.edu.itba.pod.simul.time.TimeMappers;

import com.google.common.collect.Maps;

public class SimulationImpl implements Simulation, SimulationInspector {

	private Set<SimulationEventHandler> handlers;
	private SimulationManager manager;
	
	public SimulationImpl(SimulationManager manager) {
		super();
		this.handlers = new HashSet<SimulationEventHandler>();
		this.manager = manager;
		
	}

	@Override
	public void wait(int amount, TimeUnit unit) throws InterruptedException {
		long toWait = TimeMappers.oneSecondEach(1, TimeUnit.HOURS).toMillis(amount, unit);
		Thread.sleep(toWait);

	}

	@Override
	public void add(SimulationEventHandler handler) {
		if (!handlers.contains(handler)) {
			handlers.add(handler);
		}

	}

	@Override
	public void remove(SimulationEventHandler handler) {
		handlers.remove(handler);

	}

	@Override
	public void raise(SimulationEvent event) {
		for (SimulationEventHandler h : handlers) {
			h.onEvent(event);
		}

	}

	@SuppressWarnings("unchecked")
	@Override
	public <T> T env(Class<T> param) {
		return (T) ((SimulationManagerImpl)manager).getEnviroment().get(param);
	}

	@Override
	public int runningAgents() {
		return manager.getAgents().size();
	}

}
